/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mycp1300assign1;
import java.util.ArrayList;
import java.util.Random;
/**
 *
 * @author alex jame
 */
public class Gridview {

    private static final int Endofgrid = 10;
    private int Startofgrid = 0;
    private int Boundary_One = 10;
    private int Boundary_Two = 10;
    private static final int Initial_Hor_Loca = 0;
    private static final int Boundary_Light_One = 3;
    private static final int Boundary_Light_Two = 6;
    private static final int Boundary_Light_Three = 9;
    private static final int Light_Loca = 9;
    //private static final int Light_Loca_Two = 13;
    private static final int Light_Loca_Vert = 14;
    private static final int Light_Loca_Hort = 9;
    private ArrayList<Car> carsArrVert;
    private ArrayList<Car> carsArrHort;

char[][] cell;
    Projectmodel promodel;
    int griSize;
    ArrayList<Car> carArray;

    public Gridview() {
    }

    public Gridview(Projectmodel promodel) {
        this.promodel = promodel;
        cell = new char[promodel.getGridSize()][promodel.getGridSize()];
        griSize = promodel.getGridSize();
        carArray = new ArrayList<Car>();
        carsArrVert = new ArrayList<Car>();
        carsArrHort = new ArrayList<Car>();
    }

    public void initGrid(char ch) {
        for(int i=0; i<griSize; i++) {
            for(int j=0; j<griSize; j++) {
                cell[i][j] = ch;
            }
        }
        buildHorLane();
        buildVerLane();
        setCarsGView();
    }

     public void show (){
        hortLight();
        vertLight();
        setCarsGView();
        showGrid();
    }

    public void showGrid() {
        for(int i=0; i<griSize; i++) {
            for(int j=0; j<cell.length; j++) {
                System.out.print(cell[i][j]);
            }
             System.out.println();
        }
    }

    public void buildHorLane() {
      int midd = griSize / 2;
      int horLane = promodel.getHortLanes();
      for(int i=0; i<griSize; i++) {
            for(int c=0; c<horLane; c++) {
                cell[midd+c][i] = '-';
            }
        }
    }

    public void buildVerLane() {
        int midd = griSize / 2;
        int verLane = promodel.getVertLanes();
        for(int i=0; i<griSize; i++) {
            for(int q=0; q<verLane; q++) {
                 cell[i][midd+q] = '|';
            }
        }
    }

    public void vertLight(){
    if(promodel.getTrafVertLight()== true){
        cell[Light_Loca][Light_Loca_Vert] = 'G';
    }else if (promodel.getTrafVertLight() == false){
        cell[Light_Loca][Light_Loca_Vert] = 'R';
    }
}


public void hortLight(){
    if(promodel.getTrafHortLight() == true){
        cell[Light_Loca][Light_Loca_Hort] = 'G';
    }else if (promodel.getTrafHortLight() == false){
        cell[Light_Loca][Light_Loca_Hort] = 'R';
    }
}


private void setCarsGView(){
    for (int c = 0; c < carsArrVert.size(); c++) {
        Car cars= carsArrVert.get(c);
        int x = cars.getXPost();
        int y = cars.getYPost();
        cell[x][y] = 't';
    }
    for (int c = 0; c < carsArrHort.size(); c++) {
        Car cars = carsArrHort.get(c);
        int x = cars.getXPost();
        int y = cars.getYPost();
        cell[x][y] = 'c';

    }
}

 private void addCars(){
        addCarsHort();
        addCarsVert();
        show();

    }
  private void addCarsVert(){
        for (int i = 0; i < promodel.getVertLanes(); i++) {
            Random random = new Random();
            float randomNum = random.nextFloat();
            if (randomNum < promodel.getVertProbl()){
               carsArrVert.add(new Car (Initial_Hor_Loca, Boundary_Two + i));
            }else if (randomNum > promodel.getVertLanes()){

            } moveVertCars();
        }
    }

    private void addCarsHort(){
        for (int i = 0; i < promodel.getHortLanes() ; i++) {
           Random random = new Random();
           float randomNum = random.nextFloat();
           if(randomNum < promodel.getHortProbl()){
            carsArrHort.add(new Car (Boundary_One + i, Initial_Hor_Loca));

           }else if(randomNum > promodel.getHortProbl()){
           }
           moveHortCars();
        }
    }
    public void moveCars(int numSimul){
        for (Startofgrid= 1;  Startofgrid<= numSimul; Startofgrid++) {
            if(Startofgrid > numSimul){
               Startofgrid = 1;
            }
            if(Startofgrid <= numSimul){
                    promodel.setTrafHortLight(false);
                    promodel.setTrafVertLight(true);
                if (Startofgrid <= Boundary_Light_Three) {
                            promodel.setTrafHortLight(true);
                            promodel.setTrafVertLight(false);
                    if (Startofgrid <= Boundary_Light_Two) {
                            promodel.setTrafHortLight(false);
                            promodel.setTrafVertLight(true);
                        if (Startofgrid <= Boundary_Light_One) {
                            promodel.setTrafHortLight(true);
                            promodel.setTrafVertLight(false);

                        }
                    }
                }
                    System.out.println("");
            }

            addCars();
            System.out.println("");
        }

}


    private void moveVertCars(){
        for (int ecar = 0; ecar < carsArrVert.size(); ecar++) {
            Car car = carsArrVert.get(ecar);
            int x = car.getXPost();
            if(x != Light_Loca){
                car.setXPost(x += 1);
            }
            if (x == Light_Loca){
                if(promodel.getTrafVertLight()== true){
                    car.setXPost(x +=1);
                }
            }
            if(x >= promodel.getGridSize()){
                carsArrVert.remove(car);
            }
        }

   }

    private void moveHortCars(){
        for (int i = 0; i < carsArrHort.size(); i++) {
            Car car = carsArrHort.get(i);
            int y = car.getYPost();
            if (y != Light_Loca) {
                car.setYPost(y += 1);
            }
            if (y == Light_Loca) {
                if (promodel.getTrafHortLight() == true) {
                    car.setYPost(y += 1);
                }
            }
            if (y >= promodel.getGridSize()) {
                carsArrHort.remove(car);
            }

        }
    }
    

    
}
